<?php
    class ApartmentBLL{

        //验证apartment-liat.php的输入
        public function apartmentListViewValidate($page,$pageSize,$property){
            $flag=true;
            $validate=new Validate();
            if(!$validate->regular('page',$page)){
                //echo 1;
                $flag=false;
            }
            if(!$validate->regular('page',$pageSize)){
                //echo 2;
                $flag=false;
            }
            if(!$validate->regular('page',$property)){
                //echo 3;
                $flag=false;
            }

            return $flag;
        }
        //获取公寓列表
        public function getApartmentList($page,$pageSize,$property,$search){
            $result=null;
            $apartmentDAL=new ApartmentDAL();
            $action=$this->searchPatternSelection($property,$search);
            $result=$apartmentDAL->getApartmentList($action,$page*$pageSize,$pageSize,$property,$search);

            return $result;
        }

        //获取公寓列表总页数
        public function getApartmentListPage($pageSize,$property,$search){
            $pageMax=0;
            $action=$this->searchPatternSelection($property,$search);
            $count=$this->getApartmentCount($property,$search);

            if($count>$pageSize){
                if($count%$pageSize==0){
                    $pageMax=(int)$count/$pageSize;
                }else{
                    $pageMax=((int)$count/$pageSize)+1;
                }
            }

            return $pageMax;
        }

        //获取公寓总数
        public function getApartmentCount($property,$search){
            $count=0;
            $action=$this->searchPatternSelection($property,$search);
            $apartmentDAL=new ApartmentDAL();
            $result=$apartmentDAL->getApartmentCount($action,$property,$search);
            $count=(int)$result['count'];

            return $count;
        }

        //获取公寓宿舍总数
        public function getApartmentDormitorySum($apartmentID){
            $dormitorySum=0;
            $apartmentDAL=new ApartmentDAL();
            $floorList=$apartmentDAL->getApartmentFloorInformation($apartmentID);
            foreach($floorList as $floorInfo){
                $dormitorySum+=(int)$floorInfo['rooms'];
            }

            return $dormitorySum;
        }

        //获取宿舍数量
        public function getApartmentDormitoryNumber($action,$status,$apartmentID){
            $apartmentDAL=new ApartmentDAL();
            $result=$apartmentDAL->getApartmentDormitoryNumber($action,$status,$apartmentID);
            $number=$result['count'];

            return $number;
        }

        //获取查询模式
        public function searchPatternSelection($perproty,$search){
            $pattern=0;
            if($perproty==0){
                if($search==''){
                    $pattern=1;
                }else{
                    $pattern=2;
                }
            }else{
                if($search==''){
                    $pattern=3;
                }else{
                    $pattern=4;
                }
            }

            return $pattern;
        }
        
        //获取公寓信息
        public function getApartmentInformation($dataObject){
            $message=array();
            $validate=new Validate();
            if($validate->regular("id",$dataObject->id)){
                $apartmentDAL=new ApartmentDAL();
                $apartment=$apartmentDAL->getApartmentInformation($dataObject->id);
                $floor=$apartmentDAL->getApartmentFloorInformation($dataObject->id);
                $message=array(
                    'code'=>"2001",
                    'apartment'=>$apartment,
                    'floor'=>$floor,
                    'message'=>'',
                    'url'=>"./apartment-edit.php"
                );
            }else{
                $message=array(
                    'code'=>"2004",
                    'message'=>'',
                    'url'=>"./apartment-edit.php"
                );
            }
            
            return $message;
        }

        //添加公寓
        public function addApartment($apartment){
            $number_len=(int)$apartment->number_of_rooms;
            
            $count=0;
            while($number_len>1){
                ++$count;
                $number_len/=10;
            }
            if($count<3){
                $apartment->number_len=100;
            }else{
                    $apartment->number_len=pow(10,(int)$count);
            }
            $apartment_max=$this->getApartmentIDMax();
            $apartment->apartment_id=$apartment_max+1;
            $apartmentDAL=new ApartmentDAL();
            $check=$apartmentDAL->checkValue("tb_apartment_info",array("apartment_id"=>$apartment->apartment_id,"apartment_name"=>$apartment->apartment_name));
            if(count($check)==0){
                $status=$apartmentDAL->addApartment($apartment);
                //$a=json_encode($apartment,JSON_UNESCAPED_UNICODE);
                if($status){
                    $message=array(
                        'code'=>"2001",
                        'message'=>"提交成功",
                        'url'=>"./apartment-edit.php?id=".$apartment->apartment_id
                    );
                }else{
                    $message=array(
                        'code'=>"2004",
                        'message'=>"提交失败",
                        'url'=>"./apartment-edit.php"
                    ); 
                }
            }else{
                $key=array();
                for($i=0;$i<count($check);$i++){
                    if($check[$i]=="apartment_id"){
                        $key[$i]='0';
                    }
                    if($check[$i]=="apartment_name"){
                        $key[$i]='1';
                    }
                }
                $message=array(
                    'code'=>"2003",
                    'message'=>$key,
                    'url'=>"./apartment-edit.php"
                );
            }
            return $message;
        }
        //修改公寓信息
        public function modifyApartmentInformation($dataObject){
            $apartmentError=array();
            $floorError=array();
            $message=array();
            $validateStatus=false;

            $apartment=$dataObject->apartment;
            $floors=$dataObject->floor;

            //验证公寓信息输入格式
            $apartmentError=$this->inputValidate($apartment);
            foreach($floors as $floor){
                $floorError[]=$this->inputValidate($floor);
                $validateStatus=true;
            }

            if(count($apartmentError)==0 && count($floorError,1)==count($floorError) && $validateStatus){
                $apartmentDAL=new ApartmentDAL();
                $apartmentArray=array(
                    'apartment'=>$apartmentDAL->getApartmentInformation($apartment->id),
                    'floor'=>$apartmentDAL->getApartmentFloorInformation($apartment->id)
                );
                
                $apartmentObjectOld=json_decode(json_encode($apartmentArray,JSON_UNESCAPED_UNICODE));
  
                if($apartmentDAL->updateApartmentInformation($dataObject,$apartmentObjectOld)){
                    $message=array(
                        'code'=>"2001", 
                        'message'=>'',
                        'url'=>"./apartment-edit.php"
                    );
                }else{
                    $message=array(
                        'code'=>"2004", 
                        'message'=>'',
                        'url'=>"./apartment-edit.php"
                    );
                }
            }else{
                $message=array(
                    'code'=>"2003",
                    'apartment'=>$apartmentError,
                    'floor'=>$floorError,
                    'message'=>count($floors),
                    'url'=>"./apartment-edit.php"
                );
            }
            
            return $message;
        }

        //获取公寓最大编号
        public function getApartmentIDMax(){
            $apartmentDAL=new ApartmentDAL();
            $result=$apartmentDAL->getApartmentIDMax();
            $apartmentMaxID=$result['max'];
            return $apartmentMaxID;
        }

        //获取单层楼层信息
        public function getApartmentFloorInformation($dataObject){
            $message=array();

            $error=$this->inputValidate($dataObject);
            if(count($error)==0){
                $apartmentDAL=new ApartmentDAL();
                $floor=$apartmentDAL->getApartmentFloorInformation($dataObject->id,$dataObject->floor);
                $message=array(
                    'code'=>"2001",
                    'message'=>$floor,
                    'url'=>"./apartment-edit.php"
                );
            }else{
                $message=array(
                    'code'=>"2004",
                    'message'=>$error,
                    'url'=>"./apartment-edit.php"
                );
            }

            return $message;
        }

        //获取总页数
        public function getDormitoryListPage($dataArray){
            $page=0;
            $pageSize=10;
            $apartmentDAL=new ApartmentDAL();
            $result=$apartmentDAL->getFloorDormitoryCount($dataArray->id,$dataArray->floor);
            $count=(int)$result['count'];

            if($count>$pageSize){
                if($count%$pageSize===0){
                    $page=(int)($count/$pageSize);
                }else{
                    $page=(int)($count/$pageSize)+1;
                }
                
            }
            $message=array(
                'code'=>"2001",
                'message'=>$page,
                'url'=>"./apartment-edit.php"
            );

            return $message;

        }
        //获取宿舍列表
        public function getApartmentDormitoryList($dataObject){
            $pageSize=10;
            $message=array();
            $pageNumber=$dataObject->page*$pageSize;

            $error=$this->inputValidate($dataObject);
            if(count($error)==0){
                $apartmentDAL=new ApartmentDAL();
                //$result=$apartmentDAL->getDormitoryListPage($dataObject->id,$dataObject->floor);
                $floor=$apartmentDAL->getDormitoryList($dataObject->id,$dataObject->floor,$pageNumber,$pageSize);
                $message=array(
                    'code'=>"2001",
                    'message'=>$floor,
                    'url'=>"./apartment-edit.php"
                );
            }else{
                $message=array(
                    'code'=>"2004",
                    'message'=>$error,
                    'url'=>"./apartment-edit.php"
                );
            }

            return $message;
        }

        //查找宿舍
        public function selectDormitory($dataObject){
            $apartmentDAL=new ApartmentDAL();
            $dormitory=$apartmentDAL->selectDormitory($dataObject->id,$dataObject->floor,$dataObject->dormitoryID);
            $message=array(
                'code'=>"2001",
                'message'=>$dormitory,
                'url'=>"./apartment-edit.php"
            );

            return $message;
        }

        //修改宿舍设置
        public function modifyDormitoryStting($dataObject){
            $message=array();
            $apartmentError=array();
            $dormitoryError=array();

            $apartment=$dataObject->apartment;
            $dormitories=$dataObject->dormitory;

            $apartmentError=$this->inputValidate($apartment);
            foreach($dormitories as $dormitory){
                $dormitoryError[]=$this->inputValidate($dormitory);
            }

            if(count($apartmentError)==0 && count($dormitoryError,1)==count($dormitoryError)){
                $apartmentDAL=new ApartmentDAL();
                if($apartmentDAL->modifyDormitoryStting($apartment,$dormitories)){
                    $message=array(
                        'code'=>'2001',
                        'message'=>"修改成功",
                        'url'=>'./apartment-edit.php'
                    );
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'修改失败',
                        'url'=>'./apartment-edit.php'
                    );
                }
            }else{
                $message=array(
                    'code'=>'2003',
                    'message'=>'输入错误',
                    'apartmentError'=>$apartmentError,
                    'dormitoriesError'=>$dormitoriesError,
                    'url'=>'./apartment-edit.php'
                );
            }

            return $message;
        }

        //验证输入https://gitee.com/dbge/StudentApartmentManagementSystem.git
        public function inputValidate($object){
            $error=array();
            $validate=new Validate();
            foreach($object as $key=>$value){
                if(!$validate->regular($key,$value)){
                    $error[]=$key;
                }
            }
            return $error;
        }
    }
    
?>